package bupt.party.online.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;

import bupt.party.online.dao.IBranchDao;
import bupt.party.online.dao.IStudentDao;
import bupt.party.online.model.Student;
import bupt.party.online.service.IStudentService;

public class StudentServiceImpl implements IStudentService {
	private IStudentDao studentDao;
	private IBranchDao branchDao;

	public IStudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(IStudentDao studentDao) {
		this.studentDao = studentDao;
	}
	
	
	public IBranchDao getBranchDao() {
		return branchDao;
	}

	public void setBranchDao(IBranchDao branchDao) {
		this.branchDao = branchDao;
	}

	public Student findStudentByUserIdAndPassword(String userId, String password) {
		Student student = studentDao.findStudentByUserIdAndPassword(userId, password);
		return student;
	}

	@Override
	public Student findStudentByStudentNO(String studentNO) {
		
		return studentDao.findStudentByStudentNO(studentNO);
	}

	@Override
	public boolean savePasswordByStudentNO(String studentNO, String password) {
		//先根据id查出学生，然后更新字段，最后保存
		Student student = studentDao.findStudentByStudentNO(studentNO);
		if(student!=null){
			student.setstudentPassword(password);
			studentDao.saveOrUpdateStudent(student);
			return true;
		}else{
			return false;
		}
		
		
		
	}

	@Override
	public void saveOrUpdateStudent(Student student) {
		studentDao.saveOrUpdateStudent(student);
	}

	@Override
	public List<Student> findAllStudent() {
		
		return studentDao.findAllStudent();
	}


	@Override
	public int getStudentCountByBranchId(int branchId) {
		
		return studentDao.getStudentCountByBranchId(branchId);
	}
	
	@Override
	public boolean deleteStudent(String studentNO) {
		Student student = this.findStudentByStudentNO(studentNO);
		if(student!=null){
			student.setIsDelete(1);
			return true;
		}
		
		return false;
	}

	@Override
	public List<Student> findStudentByBranchId(Integer branchId) {
		return studentDao.findStudentByBranchId(branchId);
	}

	@Override
	public List<Student> getStudentByConditions(
		
		Map<String, Object> conditions, int pageNow, int pageSize) {

		
		int firstResult = pageSize*(pageNow -1);
		
		if(firstResult < 0) firstResult = 0;
		
		if(conditions == null || conditions.size() <= 0) 
			
			return this.studentDao.getResultsByPage(firstResult, pageSize);
		
		else 
			return null;
		
	}

	@Override
	public List<Student> QueryStudentByBranchId(int branchId, int pageNow, int pageSize) {
		int firstResult = (pageNow-1)*pageSize;
		return this.studentDao.QueryStudentByBranchId(branchId, firstResult, pageSize);
	}

	@Override
	public DetachedCriteria getDetachedCriteria(Integer highLevelQuery,
			String queryContent, Integer firstpa, Integer secondpa) {
		return studentDao.getDetachedCriteria(highLevelQuery, queryContent, firstpa, secondpa);
	}

	@Override
	public int queryStudentCountWithCriteria(DetachedCriteria detachedCriteria) {
		return studentDao.queryStudentCountWithCriteria(detachedCriteria);
	}

	@Override
	public List<Student> queryStudentWithCriteria(
			DetachedCriteria detachedCriteria, int pageNow, int pageSize) {
		int firstResult = (pageNow-1)*pageSize;
		return studentDao.queryStudentWithCriteria(detachedCriteria, firstResult, pageSize);
	}

	@Override
	public Student findStudentByStudentNOAndBranchId(String studentNO,
			int branchId) {
		
		return studentDao.findStudentByStudentNOAndBranchId(studentNO, branchId);
	}

	@Override
	public Student findStudentByStudentNOAndBranchIds(String studentNO,
			Set<Integer> branch_ids) {
		
		return studentDao.findStudentByStudentNOAndBranchIds(studentNO, branch_ids);
	}

	
	
}
